#include "uart.h"

unsigned char i = 0; 
unsigned char ack[2] = {'O','K'};
unsigned char revIdx = 0;
unsigned char revBuf[5];
unsigned char revDone = 0;

float threshold = 30.0;
	
void Uart1_Isr(void) interrupt 4
{
	if(RI == 1) {
		RI = 0;
		revBuf[0] = SBUF;
		SBUF = revBuf[0];
		threshold = 1.0f * (    (revBuf[0] / 16) * 10 + (revBuf[0] % 16)    );
	}
	if(TI == 1) {
		TI = 0;
	}
}

//@11.0592MHz timer 1
void Uart_Timer_1_Init(void)	
{
	PCON |= 0x80;// 1/16 * (timer 1 freq)  //19200bps
	//PCON &= 0x7F;// 1/32 * (timer 1 freq)  //9600bps
	SCON = 0x50; //8bit uart  REN = 1	
	TMOD = 0x20; //timer 1  8bit auto reload	
	TL1 = 0xFD;	
	TH1 = 0xFD;//  11.0592MHz
	ET1 = 0; 
	ES = 1;   //enable uart interrupt	
	TR1 = 1;
	EA = 1;
}

//@11.0592MHz timer 2
void Uart_Timer_2_Init(void)	
{
	TCLK = 1; //timer 2 as uart Tx clock
	RCLK = 1; //timer 2 as uart Rx clock
	SCON = 0x50; //8bit uart REN = 1
	RCAP2L = 0xDC;//9600bps
	RCAP2H = 0xFF;//9600bps
	ET2 = 0;//disable timer 2 interrupt
	ES = 1;   //enable uart interrupt	
	TR2 = 1;// timer 2 run
	EA = 1;
}